<?php

/**
 * Reset User Password
 * 
 * @project : Loobica Ads
 * @author  : Dino
 * @date    : 22 Feb 2014
 * 
 */
if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Password extends LB_Controller {

  public function __construct() {
    parent::__construct();
    $this->prevent_signed();
  }

  /**
   * Redirect to reset function
   * 
   */
  public function index() {
    $this->redirect('password/reset');
  }

  /**
   * Get reset email (Reset Password)
   * 
   */
  public function reset() {

    // Init vars
    $user_email = null;

    // Processing user captcha security always for signup
    $user_security = $this->user_security->always()->run();

    // Checking request type
    if ($this->is_post()) {

      // Validate form
      $this->form_validation->set_rules('user_email', 'Email', 'required|valid_email|min_length[10]|max_length[50]|valid_registerd_email');
      $this->form_validation->set_rules('user_security_answer', 'Security answer', 'valid_security_answer');

      // Form success
      if ($this->form_validation->run()) {

        // Getting input values
        $user_email = trim($this->input->post('user_email', true));

        // Getting email data
        $user_data = $this->user_model->get_email($user_email, 1, true);

        // Getting email details
        if (!empty($user_data)) {

          // Create token
          $reset_token = $this->encrypt->encode(json_encode(array('type' => 'password', 'id' => $user_data['user_id'], 'password' => md5($user_data['user_password']))));

          // Send reset email
          $email_view_data = array();
          $email_view_data['reset_token'] = $reset_token;
          $email_content = $this->load->view('password/email', $email_view_data, true);
          $email_subject = lang('loobica_ads') . ' - ' . lang('reset_password');
          $this->send_email($user_email, $email_subject, $email_content);

          // Clear captcha if form submited successfully
          $this->user_security->clear();
        }
      }
    }

    // Preparing and loading view
    $view_data = array();
    $view_data['title'] = lang('password_reset');
    $view_data['user_email'] = $user_email;
    $view_data['user_security'] = $user_security;

    // Loading view
    $this->load_view('password/reset', $view_data);
  }

  /**
   * Change password
   * 
   */
  public function change() {

    $reset_token = (string) trim($this->input->get('token', true));

    // Checking token
    if (!empty($reset_token)) {

      // Decrypting token
      $token_data = json_decode($this->encrypt->decode($reset_token), true);

      // Checking token data
      if (!empty($token_data) && ($token_data['type'] == 'password') && !empty($token_data['id'])) {

        // Getting user data 
        $user_data = $this->user_model->get_user_id($token_data['id'], 1, true);

        // Checking user data
        if (!empty($user_data)) {

          // Checking token is valid
          if ($token_data['password'] == md5($user_data['user_password'])) {

            // Init vars
            $user_password = null;
            $user_password_confirm = null;

            // Form submit
            if ($this->is_post()) {

              // Validate form
              $this->form_validation->set_rules('user_password', 'Password', 'required|min_length[5]|max_length[50]|matches[user_password_confirm]');
              $this->form_validation->set_rules('user_password_confirm', 'Confirm password', 'required|min_length[5]|max_length[50]');

              // Form success
              if ($this->form_validation->run()) {

                // Getting input values
                $user_password = (string) $this->input->post('user_password', true);

                // Set new password
                $user_set = array();
                $user_set['user_password'] = $this->loobica_hash($user_password);
                $this->user_model->set_user($user_set, $user_data['user_id']);

                // Sign in user
                $this->user_session->sign_on($user_data['user_id']);

                // Redirect to home
                $this->redirect($this->redirect_to);
              }
            }

            // Preparing and loading view
            $view_data = array();
            $view_data['title'] = lang('password_change');
            $view_data['reset_token'] = $reset_token;
            $view_data['user_email'] = $user_data['user_email'];
            $view_data['user_password'] = $user_password;
            $view_data['user_password_confirm'] = $user_password_confirm;

            // Loading view
            $this->load_view('password/change', $view_data);
            return;
          }
        }
      }
    }

    // Redirect if something wrong
    $this->redirect('password/reset');
  }

}
